trigger DNC_IDNC_maskingEmailonLead on Lead (before insert,before update) {

    Boolean UserPermission = False;
    Boolean showspanish= false;
    
    // Below code if Profile Ids are used
    Set<String> ProfileIds = new Set<String>();
    for (UserAccessDNC__c UA_DNC : UserAccessDNC__c.getAll().values()){
        ProfileIds.add(UA_DNC.Profile_Ids__c.substring(0, 15));
    } 
    if(!ProfileIds.isEmpty()){
        UserPermission = ProfileIds.contains(UserInfo.getProfileId().substring(0, 15));
    }          

    if(UserInfo.getLocale()=='es_MX')
    {
        showspanish= true;
    }
/*
    Schema.DescribeSObjectResult d = Schema.SObjectType.Lead;
    Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName =  rtMapByName.get('West - Prospective Student');
    String RecTypeId  = rtByName.getRecordTypeId();*/
    for(Lead A: Trigger.New){
    if(trigger.isUpdate){
           
        
        //  Personal Email Masking:
            if(Trigger.oldMap.get(A.Id).Personal_Email_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Personal_Email_Status__c == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Personal_Email__c != Null){ A.addError('You cannot change "Email" once it\'s status is marked as Opt Out!');}     
            } else if(Trigger.oldMap.get(A.Id).Personal_Email_Status__c != 'DNC' && Trigger.newMap.get(A.Id).Personal_Email_Status__c == 'DNC') {
                A.Personal_Email__c='';                           
            } else if(Trigger.oldMap.get(A.Id).Personal_Email_Status__c  == 'DNC' && Trigger.newMap.get(A.Id).Personal_Email_Status__c  != 'DNC') {
                if(UserPermission == False){
                   A.addError('You cannot change "Email" once it\'s status is marked as Opt Out!');  
                   } else {
                    A.Personal_Email__c = A.Personal_Email_Unmasked__c;
                }                
            } else {
                A.Personal_Email_Unmasked__c= A.Personal_Email__c ;
            }        
            
         //  Work Email Masking:
            if(Trigger.oldMap.get(A.Id).Work_Email_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Work_Email_Status__c == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Work_Email__c != Null){A.addError('You cannot change "Email" once it\'s status is marked as Opt Out!');}    
            } else if(Trigger.oldMap.get(A.Id).Work_Email_Status__c != 'DNC' && Trigger.newMap.get(A.Id).Work_Email_Status__c == 'DNC') {
                A.Work_Email__c='';                           
            } else if(Trigger.oldMap.get(A.Id).Work_Email_Status__c  == 'DNC' && Trigger.newMap.get(A.Id).Work_Email_Status__c  != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Email Opt Out permissions can change "Opt Out" once it\'s status is marked as Opt Out!');
                } else {
                    A.Work_Email__c = A.Work_Email_Unmasked__c;
                }                
            } else {
                A.Work_Email_Unmasked__c= A.Work_Email__c ;
            }           
            
           //  University Email Masking:
            if(Trigger.oldMap.get(A.Id).University_Email_Status__c == 'DNC' && Trigger.newMap.get(A.Id).University_Email_Status__c == 'DNC') {
                IF(Trigger.newMap.get(A.Id).University_Email__c != Null){A.addError('You cannot change "Email" once it\'s status is marked as Opt Out!');}    
            } else if(Trigger.oldMap.get(A.Id).University_Email_Status__c != 'DNC' && Trigger.newMap.get(A.Id).University_Email_Status__c == 'DNC') {
                A.University_Email__c='';                           
            } else if(Trigger.oldMap.get(A.Id).University_Email_Status__c  == 'DNC' && Trigger.newMap.get(A.Id).Work_Email_Status__c  != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Email DNC permissions can change "Opt Out" once it\'s status is marked as Opt Out!');
                } else {
                    A.University_Email__c = A.University_Email_Unmasked__c;
                }                
            } else {
                A.University_Email_Unmasked__c= A.University_Email__c ;
            }             
            
          //  Mother's Email Masking:
            if(Trigger.oldMap.get(A.Id).Mother_s_Email_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Mother_s_Email_Status__c == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Mother_s_Email__c!= Null){A.addError('You cannot change "Email" once it\'s status is marked as Opt Out!');}    
            } else if(Trigger.oldMap.get(A.Id).Mother_s_Email_Status__c != 'DNC' && Trigger.newMap.get(A.Id).Mother_s_Email_Status__c == 'DNC') {
                A.Mother_s_Email__c='';                           
            } else if(Trigger.oldMap.get(A.Id).Mother_s_Email_Status__c  == 'DNC' && Trigger.newMap.get(A.Id).Mother_s_Email_Status__c  != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Email Opt Out permissions can change "Opt Out" once it\'s status is marked as Opt Out!');
                } else {
                    A.Mother_s_Email__c = A.Mother_s_Email_Unmasked__c;
                }                
            } else {
                A.Mother_s_Email_Unmasked__c= A.Mother_s_Email__c ;
            }            
            
            //  Father's Email Masking:
            if(Trigger.oldMap.get(A.Id).Father_s_Email_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Father_s_Email_Status__c == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Father_s_Email__c!= Null){A.addError('You cannot change "Email" once it\'s status is marked as Opt Out!');}    
            } else if(Trigger.oldMap.get(A.Id).Father_s_Email_Status__c != 'DNC' && Trigger.newMap.get(A.Id).Father_s_Email_Status__c== 'DNC') {
                A.Father_s_Email__c='';                           
            } else if(Trigger.oldMap.get(A.Id).Father_s_Email_Status__c  == 'DNC' && Trigger.newMap.get(A.Id).Father_s_Email_Status__c  != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Email Opt Out permissions can change "Opt Out" once it\'s status is marked as Opt Out!');
                } else {
                    A.Father_s_Email__c = A.Father_s_Email_Unmasked__c;
                }                
            } else {
                A.Father_s_Email_Unmasked__c= A.Father_s_Email__c ;
            }                 
            
            
            // Marketing  Email Masking:
            if(Trigger.oldMap.get(A.Id).Marketing_Email_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Marketing_Email_Status__c == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Marketing_Email__c!= Null){A.addError('You cannot change "Email" once it\'s status is marked as Opt Out!');}    
            } else if(Trigger.oldMap.get(A.Id).Marketing_Email_Status__c != 'DNC' && Trigger.newMap.get(A.Id).Marketing_Email_Status__c== 'DNC') {
                A.Marketing_Email__c='';                           
            } else if(Trigger.oldMap.get(A.Id).Marketing_Email_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Marketing_Email_Status__c != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Email Opt Out permissions can change "Opt Out" once it\'s status is marked as Opt Out!');
                } else {
                    A.Marketing_Email__c = A.Marketing_Email_Unmasked__c;
                }                
            } else {
                A.Marketing_Email_Unmasked__c= A.Marketing_Email__c ;
            }         
            
            // Other  Email Masking:
            if(Trigger.oldMap.get(A.Id).Other_Email_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Other_Email_Status__c == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Other_Email__c!= Null){A.addError('You cannot change "Email" once it\'s status is marked as Opt Out!');}    
            } else if(Trigger.oldMap.get(A.Id).Other_Email_Status__c != 'DNC' && Trigger.newMap.get(A.Id).Other_Email_Status__c== 'DNC') {
                A.Other_Email__c='';                           
            } else if(Trigger.oldMap.get(A.Id).Other_Email_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Other_Email_Status__c != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Email Opt Out permissions can change "Opt Out" once it\'s status is marked as Opt Out!');
                } else {
                    A.Other_Email__c = A.Other_Email_Unmasked__c;
                }                
            } else {
                A.Other_Email_Unmasked__c= A.Other_Email__c ;
            }                  
            
             // Other  Email 2 Masking:
            if(Trigger.oldMap.get(A.Id).Other_Email_2_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Other_Email_2_Status__c == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Other_Email_2__c!= Null){A.addError('You cannot change "Email" once it\'s status is marked as Opt Out!');}    
            } else if(Trigger.oldMap.get(A.Id).Other_Email_2_Status__c != 'DNC' && Trigger.newMap.get(A.Id).Other_Email_2_Status__c== 'DNC') {
                A.Other_Email_2__c='';                           
            } else if(Trigger.oldMap.get(A.Id).Other_Email_2_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Other_Email_2_Status__c != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Email Opt Out permissions can change "Opt Out" once it\'s status is marked as Opt Out!');
                } else {
                    A.Other_Email_2__c = A.Other_Email_2_Unmasked__c;
                }                
            } else {
                A.Other_Email_2_Unmasked__c= A.Other_Email_2__c;
            } 
                            
             

               
    }
    else if(trigger.isInsert){

        //  Personal Email Masking:
            if(A.Personal_Email_Status__c == 'DNC') {
                    A.Personal_Email_Unmasked__c= A.Personal_Email__c;
                    A.Personal_Email__c='';                           
            } else {
                A.Personal_Email_Unmasked__c= A.Personal_Email__c;
            }        
            
        //  Work Email Masking:
            if(A.Work_Email_Status__c == 'DNC') {
                    A.Work_Email_Unmasked__c= A.Work_Email__c;
                    A.Work_Email__c='';                           
            } else {
                A.Work_Email_Unmasked__c= A.Work_Email__c;
            }    
            
        //  University Email Masking:
            if(A.University_Email_Status__c == 'DNC') {
                    A.University_Email_Unmasked__c= A.University_Email__c;
                    A.University_Email__c='';                           
            } else {
                A.University_Email_Unmasked__c= A.University_Email__c;
            }         
            
        //  Mother's Email Masking:
            if(A.Mother_s_Email_Status__c == 'DNC') {
                    A.Mother_s_Email_Unmasked__c= A.Mother_s_Email__c;
                    A.Mother_s_Email__c='';                           
            } else {
                A.Mother_s_Email_Unmasked__c= A.Mother_s_Email__c;
            }     
            
        //  Father's Email Masking:
            if(A.Father_s_Email_Status__c == 'DNC') {
                    A.Father_s_Email_Unmasked__c= A.Father_s_Email__c;
                    A.Father_s_Email__c='';                           
            } else {
                A.Father_s_Email_Unmasked__c= A.Father_s_Email__c;
            }         
            
          //  Marketing Email Masking:
            if(A.Marketing_Email_Status__c == 'DNC') {
                    A.Marketing_Email_Unmasked__c= A.Marketing_Email__c;
                    A.Marketing_Email__c='';                           
            } else {
                A.Marketing_Email_Unmasked__c= A.Marketing_Email__c;
            }  
            
          //  Other Email Masking:
            if(A.Other_Email_Status__c == 'DNC') {
                    A.Other_Email_Unmasked__c= A.Other_Email__c;
                    A.Other_Email__c='';                           
            } else {
                A.Other_Email_Unmasked__c= A.Other_Email__c;
            }                                
        
         //  Other Email 2 Masking:
            if(A.Other_Email_2_Status__c == 'DNC') {
                    A.Other_Email_2_Unmasked__c= A.Other_Email_2__c;
                    A.Other_Email_2__c='';                           
            } else {
                A.Other_Email_2_Unmasked__c= A.Other_Email_2__c;
            }                                
        
                
    }
    }
}